<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 配送方式管理
 *
 * @author www.dingwei.cn
 */

class AdmShippingModel extends MY_Model{

    private $tbShipping = 'admin_shipping';
    private $tbSarea = 'admin_shipping_area';



    /**
     * 后台管理获取配送方式列表
     * @return	array
     * @author  heyu
     */
    public function adminGetList(){
        $status    = (int)get('status');

        if(!empty($status)){
            $this->db->where(array('status' => $status));
        }
        $this->db->order_by(' id desc');
        $this->db->select('id,shipping_name,shipping_desc,addtime,status');

        $data = $this->pagination($this->tbShipping);

        return $data;
    }

    /**
     * 获取全部配送方式
     * @return array
     * @author  heyu
     */
    public function getList(){
        $this->db->select('id,shipping_name')
            ->where(array('status' => 2))
            ->order_by('id asc');
        $list = $this->getAll($this->tbShipping);
        return $list;
    }

    /**
     * 获取配送方式信息
     * @param $id
     * @return array
     * @author  heyu
     */

    public function getShippingInfo($id){

        $this->db->select('shipping_name')
            ->where(array('id' => $id));

        return $this->getOne($this->tbShipping);

    }

    /**
     * 后台管理获取配送区域列表
     * @return	array
    */

    public function adminGetShippingAreaList(){
        $id    = (int)get('id');

        $this->db->where(array('shipping_id' => $id));

        $this->db->order_by(' id desc');
        $this->db->select('id,shipping_id,shipping_area_name,config,addtime,region_config');

        $data = $this->pagination($this->tbSarea);

        return $data;
    }

    /**
     * 获取所有配送区域
     *  @param $shippingId  配送方式id
     * @return	array
     * @author  heyu
     */
    public function getAllShippingArea($shippingId){

        $this->db->where(array('shipping_id' => $shippingId));

        $this->db->order_by(' id desc');
        $this->db->select('id,shipping_id,shipping_area_name,config,addtime,region_config');

        $data = $this->getAll($this->tbSarea);

        return $data;
    }

    /**
     * 保存信息
     * @param	array	$data	保存数据
     * @param	integer $id		用户id
     * @return	boolean
     * @author  heyu
     */
    public function save($data, $id = 0){

        if( ! $data) return false;
        if($id){
            $this->db->where(array('id' => $id));
            if($this->update($this->tbShipping, $data)){
                return true;
            }
        }else{
            if($id = $this->insert($this->tbShipping, $data)){
                return $id;
            }
        }
    }


    /**
     * 验证区域名称是否重复
     * @param $areaName  配送区域名称
     * @param $shippingId 配送方式id
     * @param int $notId
     * @return bool
     */
    public function shippingAreaNameIsExist($areaName, $shippingId, $notId = 0){

        if( ! $areaName || !$shippingId) return false;
        $this->db->select('id')->where(array('shipping_area_name' => $areaName, 'shipping_id' => $shippingId));
        if($notId){
            $this->db->where(array('id <>' => $notId));
        }
        $tA = $this->getOne($this->tbSarea);
        if($tA){
            return $tA['id'];
        }

        return false;
    }

    /**
     * 保存配送区域
     *
     * @param	array	$data	保存数据
     * @param	integer $id		区域id
     * @return	boolean
     */
    public function saveArea($data, $id = 0){

        if( ! $data) return false;
        if($id){
            $this->db->where(array('id' => $id));
            if($this->update($this->tbSarea, $data)){
                return true;
            }
        }else{
            if($id = $this->insert($this->tbSarea, $data)){
                return $id;
            }
        }
    }

    /**
     * 获取配送区域详情
     * @param $id
     * @return array
     * @author
     */
    public function getInfo($id){

        if(!$id) return array();

        $this->db->where(array('id'=>$id));

        $this->db->select('id,shipping_id,shipping_area_name,config,region_config');

        $data = $this->getOne($this->tbSarea);

        return $data;
    }

}